<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class MakeKkStreamByUserWallet extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'MakeKkStreamByUserWallet';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '根据用户账户金额修复金币流水';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    protected $financeKkStreamTable = 'finance_kk_stream';
    protected $financeWalletTable = 'finance_wallet';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        ini_set('memory_limit', '-1');

        $userIds = [

        ];

        do {
            $result = [];
            foreach($userIds as $userId) {
                //获取用户钱包
                $wallet = DB::table($this->financeWalletTable)->where('user_id', $userId)->first(['kk_balance']);
                $wallet = $this->objToArray($wallet);
                $kk_balance = $wallet['kk_balance'];

                //所有流水
                $streamList = DB::table($this->financeKkStreamTable)
                    ->select('stream_id','kk_balance','kk_value','change_time','user_id')
                    ->where('user_id', $userId)
                    ->orderBy('stream_id', 'desc')
                    ->get();
                $streamList = $this->objToArray($streamList);
                if(empty($streamList)) {
                    $result['没有流水'][] = $userId;
                    continue;
                }
                if($streamList[0]['kk_balance'] != $kk_balance) {
                    $result['最后记录金额不等于钱包'][] = $userId;
                    continue;
                }
                $num = count($streamList);

                //整理可用值
                $newList = [];
                $flag = 0;
                foreach($streamList as $k2=>$v2) {
                    if($v2['kk_value']<0) {
                        continue;
                    }
                    $item['stream_id'] = $v2['stream_id'];
                    $item['kk_balance'] = $v2['kk_balance'];
                    $item['old_kk_value'] = $v2['kk_value'];
                    if(isset($streamList[$k2+1])) {
                        $item['kk_value'] = $v2['kk_balance'] - $streamList[$k2+1]['kk_balance'];
                    }else{
                        $item['kk_value'] = $v2['kk_balance'];
                    }

                    if($item['kk_value']<0) {
                        $flag = 1;
                    }
                    if($item['kk_value']!=$v2['kk_value']) {
                        $newList[] = $item;
                    }
                }
                if($flag == 1) {
                    $result['扣款数据异常'][] = $userId;
                    $newList = [];
                    continue;
                }
                unset($streamList,$wallet);

                foreach($newList as $v3) {
                    DB::table($this->financeKkStreamTable)->where('stream_id', $v3['stream_id'])->update([
                        'kk_value' => $v3['kk_value'],
                    ]);
                }
                $result['修复了的数据'][] = $userId;
                unset($newList);
            }
            print_r($result);

        } while (false);

    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }

}